//
//  map_demo.cpp
//  cpp_learn
//
//  Created by 李贺 on 2021/12/30.
//

#include "map_demo.hpp"

namespace map_test_demo {

bool func_compare(char lhs, char rhs) {
    return lhs < rhs;
}

using FuncComp = bool (*)(char,char);

typedef bool FuckFunc(char,char);

typedef bool (*FuckFuncp)(char,char);

struct classCompare {
    bool operator() (const char &lhs, const char &rhs) const {
        return lhs < rhs;
    }
};


void test_init() {
    map<char, int> first;
    first['a']=10;
    first['b']=30;
    first['c']=50;
    first['d']=70;
    
    map<char, int> second(first.begin(),first.end());
    map<char, int> third(second);
    
    map<char, int, classCompare> forth;
    
    FuncComp fun_comp = func_compare;
    FuckFunc *fun1 = func_compare;
    FuckFuncp fun2 = func_compare;
    map<char, int, FuncComp> fifth(fun2);
}


void test_op() {
    map<string, int> first;
    first["name"] = 999;
    first["like"] = 888;
    
    pair<string, int> haha("kk", 777);
    first.insert(haha);
    
    cout << first["name"] << '\n';
    cout << first["like"] << '\n';
    cout << first.at("name") << endl;
    cout << first.at("kk") << endl;
}

void test_insert() {
    map<char, int> mymap;
    mymap.insert(pair<char, int>('a',10));
    mymap.insert(pair<char, int>('b',20));
    
    pair<map<char, int>::iterator, bool> ret;
    ret = mymap.insert(pair<char, int>('c',30));
    
    if (ret.second == false) {
        cout << "element c already existed" << endl;
        std::cout << " with a value of " << ret.first->second << '\n';
    }
    
    map<char, int>::iterator it = mymap.begin();
    // insert不会改变原来的key对应的值
    mymap.insert(it, pair<char, int>('c',300));
    mymap.insert(it, pair<char, int>('b',200));
    // 这样插入会改变原来的key 对应的值
    mymap['b'] = 200;
    mymap['c'] = 300;
    // third insert function version (range insertion):
    std::map<char,int> anothermap;
    anothermap.insert(mymap.begin(),mymap.find('c'));
    
    // showing contents:
    std::cout << "mymap contains:\n";
    for (it=mymap.begin(); it!=mymap.end(); ++it) {
        std::cout << it->first << " => " << it->second << '\n';
    }
        

    std::cout << "anothermap contains:\n";
    for (it=anothermap.begin(); it!=anothermap.end(); ++it) {
        std::cout << it->first << " => " << it->second << '\n';
    }
}

void test_erase() {
    
    map<char, int> mymap;
    mymap.insert(pair<char, int>('a',10));
    mymap.insert(pair<char, int>('b',20));
    mymap.insert(pair<char, int>('d',40));
    
    pair<map<char, int>::iterator, bool> ret;
    ret = mymap.insert(pair<char, int>('c',30));
    
    cout << "map contains" << endl;
    for (auto it = mymap.begin(); it != mymap.end(); ++it) {
        cout << it->first << "==>" << it->second << endl;
    }
    cout << endl;
    
    mymap.erase(mymap.begin());
    map<char, int>::size_type size = mymap.erase('b');
    cout << size << endl;
    
    map<char, int>::size_type size1 = mymap.erase('f');
    cout << size1 << endl;
    
    mymap.erase(mymap.begin(), mymap.end());
    
    cout << "map contains" << endl;
    for (auto it = mymap.begin(); it != mymap.end(); ++it) {
        cout << it->first << "==>" << it->second << endl;
    }
    cout << endl;
}

}


